package com.example.webbrowser.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.R.*;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.webbrowser.Entity.PopularWeb;
import com.example.webbrowser.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/*
    热门网站的适配器
    author：林泽平
 */
public class WebAdapter extends RecyclerView.Adapter<WebAdapter.ViewHolder> {
    private List<PopularWeb> popularWebList;

    private Context context;

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView webImage;
        TextView webName;

        public ViewHolder(View view){
            super(view);
            webImage = (ImageView) view.findViewById(R.id.web_image);
            webName = (TextView) view.findViewById(R.id.web_name);
        }
    }

    public WebAdapter(Context context, ArrayList<PopularWeb> popularWebList){
        this.context = context;
        this.popularWebList = popularWebList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.web_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PopularWeb pw = popularWebList.get(position);
        holder.webImage.setImageResource(pw.getImageId());
        holder.webName.setText(pw.getName());
    }

    @Override
    public int getItemCount() {
        return popularWebList.size();
    }
}
